package ham.es;

import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

import java.util.List;
import java.util.Map;

public class GetDoc {

	public static void main(String[] args) {
		TransportClient client = EsClient.getClient();
		try {
			SearchResponse response = client.prepareSearch("products")	// 索引
			        .setQuery(QueryBuilders.matchQuery("condition", "房社保")) // 全局模糊查询条件
			        .setFrom(0).setSize(60).get();
			int i=0;
			for (SearchHit hit : response.getHits().getHits()) {
				i++;
				Map<String,Object> map = hit.getSourceAsMap();
				System.out.println(map.get("id")+":"+map.get("name"));
			}
			System.out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}




		// 验证分词器
		AnalyzeRequest analyzeRequest = new AnalyzeRequest("products")
				.text("房社保")
				.analyzer("ik_max_word");

		List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices()
				.analyze(analyzeRequest)
				.actionGet()
				.getTokens();

		for (AnalyzeResponse.AnalyzeToken token : tokens) {
			System.out.println(token.getTerm());
		}
	}
}
